﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluggableModulesInterface;
using System.Threading;
using SMSROUTER.Business;
using Utils;
using PluggableModulesRouter;
using Settings;

namespace StaticThreadManagement
{
    class GCThreadManage : WorkingBaseTimer
    {
        protected override int DUETIME { get { return 1000; } }
        protected override int PERIOD { get { return 60000; } }

        protected override void LoopWorking()
        {
            int sync = Interlocked.CompareExchange(ref syncPoint, 1, 0);
            if (sync == 0)
            {
                GC.SuppressFinalize(this);
                GC.Collect();
            }
        }

        protected override void HandleException(Exception ex, string logType)
        {
            string hashEx = ex.Message.ToMD5Hash();
            if (!currentException.ContainsKey(hashEx))
            {
                currentException.Add(hashEx, DateTime.Now);
                Log.InsertLog(ex, null, logType, null);
            }
            else if (DateTime.Now - currentException[hashEx] >= TimeSpan.FromSeconds(AppSetting.AlertRouterDuration))
            {
                Log.InsertLog(ex, null, logType, null);
                currentException[hashEx] = DateTime.Now;
            }
            if (currentException.Count > 1000)
                currentException.Clear();
        }
    }
}
